package com.km.job;

import com.km.config.Config;
import com.km.entity.ArticleMd;
import com.km.entity.ArticleTags;
import com.km.entity.Tags;
import com.km.repository.ArticleMdRepository;
import com.km.repository.ArticleTagsRepository;
import com.km.repository.TagsRepository;
import com.km.util.ArticleUtil;
import com.km.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2017/10/12.
 */
@Component
@Slf4j
public class ScheduleJob {

    @Resource
    private Config config;
    @Resource
    private ArticleMdRepository articleMdRepository;
    @Resource
    private TagsRepository tagsRepository;
    @Resource
    private ArticleTagsRepository articleTagsRepository;

    private boolean isRun = true;

    /**
     * 每天2点
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void day() {

    }

    @Scheduled(fixedRate = 5 * 1000)
    public void sync() {
        if (!isRun) return;
        List<ArticleMd> articleMdList = new ArrayList<>();
        List<File> fileList = (List<File>) FileUtils.listFiles(new File(config.getArticlePath()), new String[]{"md"}, true);
        fileList.forEach(file -> {
            System.out.println(file.getName());
            try {
                ArticleMd articleMd = new ArticleMd();
                ArticleUtil.splitWord(articleMd, file.getName());
                ArticleUtil.mdToHtml(articleMd, file.getAbsolutePath());
                String md5 = MD5Util.getFileMd5(file.getAbsolutePath());
                articleMd.setMd5(md5);
                articleMd.setCreateTime(new Date());
                articleMd.setUpdateTime(new Date());
                articleMdList.add(articleMd);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        articleMdList.forEach(articleMd -> {
            List<String> tagsList = articleMd.getTagsList();
            ArticleMd articleMd1 = articleMdRepository.findArticleMdByTitle(articleMd.getTitle());
            if (articleMd1 == null) {
                articleMdRepository.save(articleMd);
                tagsList.forEach(tagsStr -> {
                    Tags tags = tagsRepository.findTagsByName(tagsStr.trim());
                    if (tags == null){
                        tags = new Tags(tagsStr.trim(), tagsStr.trim().toLowerCase());
                        tagsRepository.save(tags);
                    }
                    articleTagsRepository.save(new ArticleTags(articleMd.getId(), tags.getId()));
                });
                log.info("新建文章: {}", articleMd.getTitle());
            } else {
                if (!articleMd1.getMd5().equals(articleMd.getMd5())) {
                    articleMd1.update(articleMd);
                    articleMdRepository.save(articleMd1);
                    tagsList.forEach(tagsStr -> {
                        Tags tags = tagsRepository.findTagsByName(tagsStr);
                        articleTagsRepository.deleteByTagsId(tags.getId());
                        articleTagsRepository.save(new ArticleTags(articleMd1.getId(), tags.getId()));
                    });
                    log.info("更新文章: {}", articleMd.getTitle());
                }
            }
        });
        isRun = false;
    }

}
